package com.mv.spring.starter;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.mv.spring.bean.BeanFactory;
import com.mv.spring.core.ClassScanner;
import com.mv.spring.web.handler.HandlerManager;
import com.mv.spring.web.server.TomcatServer;
import com.mv.spring.web.servlet.DispatcherServlet;

public class SpringApplication {

    private static final Logger log = LoggerFactory.getLogger(SpringApplication.class);
    
    public static void run(Class<?> cls, String[] args) {
        TomcatServer tomcatServer = new TomcatServer();
        try {
            // 启动 tomcat 服务
            tomcatServer.startServer();
            // 启动 内部tomcat 服务
            //tomcatServer.startInner();
            // 扫描类
            log.info("spring 扫描类 scannerCLasses");
            List<Class<?>> classList = new ClassScanner().scannerCLasses(cls.getPackage().getName());
            log.info("spring 扫描类列表:" +  classList.toString());
            // 初始化 Bean 工厂,初始化 AOP，这里使用了 JDK 动态代理，
            // Bean工厂第一次初始化后，使用代理类的对象来覆盖 Bean 工厂中的对应对象
            log.info("spring 注册对象 initBean" );
            BeanFactory.initBean(classList);
            log.info("spring 注册对象列表" + BeanFactory.listBeans());
           
            // Handler 管理器实例化所有的映射处理器，并绑定到类
            log.info("spring 扫描注册方法 resolveMappingHandler");
            HandlerManager.resolveMappingHandler(classList);
    		log.info("tomcat mv service list:" + HandlerManager.mappingHandlerList.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
